#include "d3dUtility.h"
IDirect3DDevice9* Device = 0;

IDirect3DVertexBuffer9* Quad = 0;
IDirect3DVertexBuffer9* Rect = 0;
IDirect3DTexture9* Tex = 0;
struct Vertex
{
	float _x, _y, _z;
	float _nx, _ny, _nz;
	float _u, _v;
	static const DWORD FVF;
	Vertex(float p0, float p1, float p2,
			float p3, float p4, float p5,
			float p6, float p7):_x(p0),_y(p1),_z(p2),_nx(p3), _ny(p4), _nz(p5), _u(p6), _v(p7){}
};
const DWORD Vertex::FVF = D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1;

struct ColorVertex
{
	float _x, _y, _z;
	D3DCOLOR color;
	static const DWORD FVF;
	ColorVertex(float x, float y, float z, D3DCOLOR c):_x(x), _y(y), _z(z), color(c){}
};
const DWORD ColorVertex::FVF = D3DFVF_XYZ|D3DFVF_DIFFUSE;

bool Setup()
{
	Device->CreateVertexBuffer(6*sizeof(Vertex), D3DUSAGE_WRITEONLY, Vertex::FVF,
					D3DPOOL_MANAGED, &Quad, 0);
	Vertex* v;
	Quad->Lock(0,0,(void**)&v, 0);

	v[0] = Vertex(-1.0f,-1.0f,1.25f, 0.0f, 0.0f,-1.0f, 0.0f,3.0f);
	v[1] = Vertex(-1.0f, 1.0f,1.25f, 0.0f,0.0f,-1.0f, 0.0f,0.0f);
	v[2] = Vertex( 1.0f, 1.0f,1.25f, 0.0f,0.0f,-1.0f, 3.0f,3.0f);
	v[3] = Vertex(-1.0f,-1.0f,1.25f, 0.0f,0.0f,-1.0f, 0.0f,3.0f);
	v[4] = Vertex( 1.0f, 1.0f,1.25f, 0.0f,0.0f,-1.0f, 3.0f,3.0f);
	v[5] = Vertex( 1.0f,-1.0f,1.25f, 0.0f,0.0f,-1.0f, 3.0f,3.0f);
	Quad->Unlock();


	D3DXCreateTextureFromFile(Device, "logo.bmp", &Tex);
	Device->SetTexture(0, Tex);
	Device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
	Device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
	Device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_POINT);
	
	Device->CreateVertexBuffer(6*sizeof(ColorVertex), D3DUSAGE_WRITEONLY, ColorVertex::FVF,
		D3DPOOL_MANAGED, &Rect, 0);
	ColorVertex* v2;
	Rect->Lock(0, 0, (void**)&v2, 0);
	v2[0] = ColorVertex(-2.0f, -2.0f, 5.0f,D3DCOLOR_XRGB(255,0,0));
	v2[1] = ColorVertex(-2.0f, 2.0f, 5.0f,D3DCOLOR_XRGB(0,255,0));
	v2[2] = ColorVertex(2.0f, 2.0f, 5.0f,D3DCOLOR_XRGB(0,0,255));

	v2[3] = ColorVertex(-2.0f, -2.0f, 5.0f,D3DCOLOR_XRGB(255,0,0));
	v2[4] = ColorVertex(2.0f, 2.0f, 5.0f,D3DCOLOR_XRGB(0,0,255));
	v2[5] = ColorVertex(2.0f, -2.0f, 5.0f,D3DCOLOR_XRGB(0,255,0));
	Rect->Unlock();

	D3DXVECTOR3 position(0.0f, 0.0f, -10.0f);
	D3DXVECTOR3 target(0.0f, 0.0f, 1.0f);
	D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
	D3DXMATRIX V;
	D3DXMatrixLookAtLH(&V, &position, &target, &up);
	Device->SetTransform(D3DTS_VIEW, &V);

	Device->SetRenderState(D3DRS_LIGHTING, false);

	D3DXMATRIX proj;
	D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI*0.5f, 16/9/*(float)Width/(float)Height*/, 1.0f, 1000.0f);
	Device->SetTransform(D3DTS_PROJECTION, &proj);

	return true;
};

void CleanUp()
{
}

bool Display(float timeDelta)
{
	if (Device)
	{
		Device->Clear(0, 0, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
			0x00000000, 1.0f, 0);
		Device->BeginScene();
// 		Device->SetStreamSource(0, Quad, 0, sizeof(Vertex));
// 		Device->SetFVF(Vertex::FVF);
// 		Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);

		Device->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_GOURAUD);

// 		D3DXMATRIX Tz;
// 		D3DXMatrixTranslation(&Tz, 0.0f, 0.0f, 10.0f);
// 		Device->SetTransform(D3DTS_WORLD, &Tz);

		D3DXMATRIX Rz;
		static float z = 0.0f;   
		D3DXMatrixRotationZ(&Rz, z);   
		z += timeDelta;   
		Device->SetTransform(D3DTS_WORLD, &Rz);   

		Device->SetStreamSource(0, Rect, 0, sizeof(ColorVertex));
		Device->SetFVF(ColorVertex::FVF);
		Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0,2);
		Device->EndScene();
		Device->Present(0,0,0,0);
	}
	return true;
}

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, 
				    LPSTR lpCmdLine, int nShowCmd )
{
	if (!d3d::InitD3D(hInstance, 800, 600, true, D3DDEVTYPE_HAL, &Device))
	{
		::MessageBox(0, "InitD3D FAILED", 0, 0);
		return 0;
	}
	if (!Setup())
	{
		::MessageBox(0, "SETUP FAILED", 0, 0);
		return 0;
	}
	d3d::EnterMsgLoop(Display);
	CleanUp();
	Device->Release();
	return 0;
}